import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)
const Login=()=>import("../components/Login.vue");
const Home=()=>import("../components/Home.vue");
const Hello=()=>import("@/pages/Hello.vue");
const Users=()=>import("@/pages/Users.vue");
const Rights=()=>import("@/pages/Rights.vue")
const Roles=()=>import("@/pages/Roles.vue")
const GoodsType=()=>import("@/pages/Goods_type.vue")
const Params=()=>import("@/pages/Params.vue")
const Goods=()=>import("@/pages/Goods.vue")
const GOOdsAdd=()=>import("@/pages/Goods_Add.vue")
const Orders=()=>import("@/pages/Orders.vue")
const Reports=()=>import("@/pages/Reports.vue")
const routes=[
  {path:"/",redirect:"/login"},
  {path:"/login",component:Login},
  {path:"/home",component:Home,children:[
    {path:"",redirect:"/home/hello"},
    {path:"hello",component:Hello},
    {path:"users",component:Users},
    {path:"rights",component:Rights},
    {path:"roles",component:Roles},
    {path:"categories",component:GoodsType},
    {path:"params",component:Params},
    {path:"goods",component:Goods},
    {path:"goodsadd",component:GOOdsAdd},
    {path:"orders",component:Orders},
    {path:"reports",component:Reports}
  ]}
]
const router=new Router({
  mode:"history",
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path === '/login') 
  {
    return next(); 
  }
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) {
    return next('/login');
  }
  next();
})
export default router;